#include "stdafx.h" 
#include <windows.h>
#include <shellapi.h>
#include <msclr\marshal.h>
#include <strsafe.h>

using namespace System;
using namespace System::IO;
using namespace msclr::interop;

#include "FileOperationCmds.h"


int cFileOperationCmds::CopyFolder(String^ sourcePath, String^ destinationPath)
{
    SHFILEOPSTRUCT sfo;
    marshal_context^ context = gcnew marshal_context();

    ZeroMemory (&sfo,sizeof(SHFILEOPSTRUCT));

    LPTSTR srcPath = (LPTSTR)(context->marshal_as<const TCHAR*>(sourcePath + "\\*  "));
    srcPath[sourcePath->Length + 1] = 0;
    srcPath[sourcePath->Length + 2] = 0;
    LPTSTR desPath = (LPTSTR)(context->marshal_as<const TCHAR*>(destinationPath+ "\\  "));
    desPath[destinationPath->Length] = 0;
    desPath[destinationPath->Length + 1] = 0;

    sfo.pFrom  = srcPath;
    sfo.pTo    = desPath;
    sfo.hwnd = NULL;
    sfo.wFunc = FO_COPY;
    sfo.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;

    return (SHFileOperation(&sfo));
}

int cFileOperationCmds::RenameFolder(String^ sourcePath, String^ destinationPath)
{
    SHFILEOPSTRUCT sfo;
    marshal_context^ context = gcnew marshal_context();

    ZeroMemory (&sfo,sizeof(SHFILEOPSTRUCT));

    LPTSTR srcPath = (LPTSTR)(context->marshal_as<const TCHAR*>(sourcePath + "\\*  "));
    srcPath[sourcePath->Length + 1] = 0;
    srcPath[sourcePath->Length + 2] = 0;
    LPTSTR desPath = (LPTSTR)(context->marshal_as<const TCHAR*>(destinationPath+ "\\  "));
    desPath[destinationPath->Length] = 0;
    desPath[destinationPath->Length + 1] = 0;

    sfo.pFrom  = srcPath;
    sfo.pTo    = desPath;
    sfo.hwnd = NULL;
    sfo.wFunc = FO_RENAME;
    sfo.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;

    return (SHFileOperation(&sfo));
}

int cFileOperationCmds::RemoveFolder(String^ path)
{
    SHFILEOPSTRUCT sfo;
    marshal_context^ context = gcnew marshal_context();

    ZeroMemory (&sfo,sizeof(SHFILEOPSTRUCT));

    LPTSTR srcPath = (LPTSTR)(context->marshal_as<const TCHAR*>(path + "\\*  "));
    srcPath[path->Length + 1] = 0;
    srcPath[path->Length + 2] = 0;
//     LPTSTR desPath = (LPTSTR)(context->marshal_as<const TCHAR*>("  "));
//     desPath[0] = 0;
//     desPath[1] = 0;

    sfo.pFrom  = srcPath;
    sfo.pTo    = L"";
    sfo.hwnd = NULL;
    sfo.wFunc = FO_DELETE;
    sfo.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;

    return (SHFileOperation(&sfo));
}

String^ cFileOperationCmds::FindFolder(String^ dirPath, String^ partialFolderName)
{
    WIN32_FIND_DATA ffd;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    TCHAR szDir[MAX_PATH];
    marshal_context^ context = gcnew marshal_context();

    StringCchCopy(szDir, MAX_PATH, context->marshal_as<const TCHAR*>(dirPath));
    StringCchCat(szDir, MAX_PATH, TEXT("\\*"));

    hFind = FindFirstFile(szDir, &ffd);
    if (INVALID_HANDLE_VALUE == hFind) 
    {
        return ("");
    } 

    String^ folderName = "";
    do 
    {
        if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            String^ tempFolderName = gcnew String(ffd.cFileName, 0, MAX_PATH);
            tempFolderName = tempFolderName->Substring(0, tempFolderName->IndexOf(L''+L''));
            if (true == tempFolderName->ToUpper()->Contains(partialFolderName->ToUpper()))
            {
                folderName = dirPath + "\\" + tempFolderName;
                break;
            }
        }
    } while (FindNextFile(hFind, &ffd) != 0);
    FindClose(hFind);

    return(folderName);
}

String^ cFileOperationCmds::ListAllFolderName(String^ dirPath, String^ partialFolderName, int &dirCnt)
{
    WIN32_FIND_DATA ffd;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    TCHAR szDir[MAX_PATH];
    marshal_context^ context = gcnew marshal_context();

    StringCchCopy(szDir, MAX_PATH, context->marshal_as<const TCHAR*>(dirPath));
    StringCchCat(szDir, MAX_PATH, TEXT("\\*"));

    hFind = FindFirstFile(szDir, &ffd);
    if (INVALID_HANDLE_VALUE == hFind) 
    {
        return ("");
    } 

    String^ listFolderNames = "";
    dirCnt = 0;

    do 
    {
        if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            String^ tempFolderName = gcnew String(ffd.cFileName, 0, MAX_PATH);
            tempFolderName = tempFolderName->Substring(0, tempFolderName->IndexOf(L''+L''));
            if (true == tempFolderName->ToUpper()->Contains(partialFolderName->ToUpper()))
            {
                listFolderNames += tempFolderName + ",";
                dirCnt++;
            }
        }
    } while (FindNextFile(hFind, &ffd) != 0);
    FindClose(hFind);

    return(listFolderNames);
}

String^ cFileOperationCmds::FindFile(String^ dirPath, String^ partialFileName)
{
    WIN32_FIND_DATA ffd;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    TCHAR szDir[MAX_PATH];
    marshal_context^ context = gcnew marshal_context();

    StringCchCopy(szDir, MAX_PATH, context->marshal_as<const TCHAR*>(dirPath));
    StringCchCat(szDir, MAX_PATH, TEXT("\\*"));

    hFind = FindFirstFile(szDir, &ffd);
    if (INVALID_HANDLE_VALUE == hFind) 
    {
        return ("");
    } 

    String^ fileName = "";
    do 
    {
        if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
        {
            String^ tempFileName = gcnew String(ffd.cFileName, 0, MAX_PATH);
            tempFileName = tempFileName->Substring(0, tempFileName->IndexOf(L''+L''));
            if (true == tempFileName->Contains(partialFileName))
            {
                fileName = dirPath + "\\" + tempFileName;
                break;
            }
        }
    } while (FindNextFile(hFind, &ffd) != 0);
    FindClose(hFind);

    return(fileName);
}


